/*
@author: stanfor
@date: 2022/8/20 12:06
@Version: 1.0
@last modify time : 2022/8/20 12:06
*/

import React, { isValidElement } from 'react';
import { Button } from 'antd';
import { EventBtnConfigEnum, EventBtnTypeEnum, EventBtnLayoutEnum } from '@C/base/SForm/constant';
import './index.less';

/** SForm 组件说明
* @author: JasonStandFor
* @description Desc:: 'props'
* @submit {antd.ButtonProps} 来自 antd.ButtonProps
* @submitBtnText {String} 修改提交按钮文字
* @hideSubmitBtn {Boolean} 隐藏提交按钮
* @cancel {antd.ButtonProps} 来自 antd.ButtonProps
* @cancelBtnText {String} 修改返回按钮文字
* @hideCancelBtn {Boolean} 隐藏返回按钮
* @textAlign {String} 按钮对齐方式(可选 left|right|center)
* @style {Object} 按钮样式
* @extraBtn {ReactDOM} 扩展按钮，需要添加额外的按钮时可用到
* @return FooterEventBtn
**/
const EventBtnComponent = (props) => {
  const {
    layout, footerBtnType = 'confirm', form, useForm, onSubmit, openSubmitCallBackClear, onCancel,
    openCancelCallBackClear, eventBtnConfig = {}
  } = props;
  const {
    submit, submitBtnText, hideSubmitBtn = false, cancel, cancelBtnText, hideCancelBtn = false,
    textAlign, style, extraBtn
  } = eventBtnConfig;

  return (
    <div
      className="base-component-super-form-footer-event-btn"
      style={{
        justifyContent: EventBtnConfigEnum[textAlign],
        margin: EventBtnLayoutEnum[layout],
        ...style,
      }}
    >
      {
        hideSubmitBtn || (
          <Button
            className="base-component-super-form-footer-event-btn-submit"
            type="primary"
            onClick={() => {
              (form || useForm).validateFields()
                .then((formValues) => {
                  try {
                    if (!onSubmit) {
                      throw new Error('unexpected: SForm 组件需要传递一个为 Function 类型 onSubmit 属性');
                    }
                    if (footerBtnType !== 'search' && openSubmitCallBackClear) {
                      (form || useForm).resetFields();
                    }
                    onSubmit(formValues, () => (form || useForm));
                  } catch (e) {
                    console.error(e);
                  }
                })
            }}
            {...submit}
          >
            { submitBtnText || EventBtnTypeEnum[footerBtnType].leftBtnText }
          </Button>
        )
      }
      {
        hideCancelBtn || (
          <Button
            className="base-component-super-form-footer-event-btn-cancel"
            type="primary"
            ghost
            onClick={() => {
              try {
                if (!onCancel) {
                  if (footerBtnType === 'search') {
                    (form || useForm).resetFields();
                    return;
                  }
                  throw new Error('unexpected: SForm 组件需要传递一个为 Function 类型 onCancel 属性');
                }
                if (openCancelCallBackClear || footerBtnType === 'search') {
                  (form || useForm).resetFields();
                }
                onCancel(() => (form || useForm));
              } catch (e) {
                console.error(e);
              }
            }}
            {...cancel}
          >
            { cancelBtnText || EventBtnTypeEnum[footerBtnType].rightBtnText }
          </Button>
        )
      }
      { (isValidElement(extraBtn) && extraBtn) }
    </div>
  )
};

const OtherEvent = () => {
  return <></>;
};

const FooterEventBtn = (props) => {
  const { footerBtnType = 'confirm' } = props;

  try {
    if (typeof footerBtnType !== 'string') {
      throw new Error('unexpected: SForm 组件 footerBtnType 属性可能需要一个 String 类型');
    }
    if (/submit|confirm|search|import|export/img.test(footerBtnType)) {
      return <EventBtnComponent { ...props } />;
    } else {
      throw new Error('unexpected: 未找到匹配项, 请检查 SForm 组件传入的 footerBtnType 属性是否正确');
    }
  } catch (err) {
    console.error(err);
  }
  return <OtherEvent />;
};

export default FooterEventBtn;
